package org.ajax.dao.impl;

import org.ajax.dao.CommdityDAO;
import org.ajax.entity.Commodity;
import org.ajax.util.DbUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/*
 *
 *@author XCH
 *@date2024 - 11 - 20
 *
 */
public class CommdityDAOImpl implements CommdityDAO {
    @Override
    public int insertGood(Commodity Commodity) {
        int result = 0;
        String sql = "INSERT INTO goods(good_id,good_name,good_price,good_date,good_status) VALUES(?,?,?,?,?)";
        Connection conn = DbUtil.getConnection();
        PreparedStatement pst = null;
        try {
            pst = conn.prepareStatement(sql);
            pst.setInt(1, Commodity.getId());
            pst.setString(2, Commodity.getName());
            pst.setDouble(3, Commodity.getPrice());
            pst.setDate(4, (Date) Commodity.getDate());
            pst.setInt(5, Commodity.getStatus());
            result = pst.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return result;
    }

    @Override
    public int deleteGood(String goodId) {
       int result = 0;
       String sql = "DELETE FROM goods WHERE good_id = ?";
       try (Connection conn = DbUtil.getConnection();
           PreparedStatement pst = conn.prepareStatement(sql)) {
           pst.setString(1, goodId);
           result = pst.executeUpdate();
       } catch (SQLException e) {
           throw new RuntimeException(e);
       }
       return result;
    }

    @Override
    public int updateGood(Commodity commodity) {
        int result = 0;
        String sql = "UPDATE goods SET good_name = ?, good_price = ?, good_date = ?, good_status = ? WHERE good_id = ?";
        try (Connection conn = DbUtil.getConnection();
             PreparedStatement pst = conn.prepareStatement(sql)) {
            pst.setString(1, commodity.getName());
            pst.setDouble(2, commodity.getPrice());
            java.sql.Date sqlDate = new java.sql.Date(commodity.getDate().getTime());
            pst.setDate(3, sqlDate);
            pst.setInt(4, commodity.getStatus());
            pst.setInt(5, commodity.getId());

            result = pst.executeUpdate();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


    @Override
    public List<Commodity> selectGood() {
        List<Commodity> commodityList = new ArrayList<>();
        DbUtil dbUtil = new DbUtil();
        String sql = "SELECT * FROM goods";
        try (Connection conn = dbUtil.getConnection();
             PreparedStatement pst = conn.prepareStatement(sql);
             ResultSet rst = pst.executeQuery()) {

            while (rst.next()) {
                int id = rst.getInt("good_id");
                String name = rst.getString("good_name");
                double price = rst.getDouble("good_price");
                java.sql.Date date = rst.getDate("good_date");
                int status = rst.getInt("good_status");

                Commodity commodity = new Commodity(id, name, price, date, status);
                commodityList.add(commodity);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return commodityList;
    }
}
